<?php


namespace App\Exports;


use App\Models\Asset;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class AssetExport implements FromCollection, WithHeadings
{

    /**
     * @return Collection
     */
    public function collection()
    {
        return Asset::all()->map(function(Asset $asset){
            return [
                $asset->id,
                $asset->name,
                $asset->product_model_number,
                get($asset, "category.name"),
                get($asset, "subCategory.name"),
                get($asset, "brand.name"),
                $asset->sn,
                get($asset, "status.name"),
                get($asset, "affiliation.name"),
                get($asset, "keeper.name"),
                $asset->marking_price,
                $asset->bid_price,
                $asset->bid_date,
                get($asset, "vendor.name"),
                get($asset, "location.name")
            ];
        });
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            '序号',
            '名称',
            '物品编码',
            '主类',
            '副类',
            '品牌',
            'SN序列号',
            '状态',
            '归属',
            '保管人',
            '市场价',
            '购入价',
            '购入日期',
            '供应商',
            '仓库',
        ];
    }

}
